package com.hmyd.hmyd_android.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.VerifyCode;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.PreferencesUtils;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 设置交易密码
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_trading_password)
public class TradingPasswordActivity extends BasicActivity implements OnClickListener {
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.phone)
	private EditText phone;
	@ViewInject(R.id.et_code)
	private EditText et_code;
	private Button btn_get_code;

	private EditText password;
	private EditText password_re;

	private Button save;

	private String mCodeId;

	private final int SEND_REGISTER_VERIFY_CODE = 0;
	private final int SUBMIT = 1;

	public static Intent getIntent(Context context, String title) {
		Intent intent = new Intent(context, TradingPasswordActivity.class);
		intent.putExtra("title", title);
		return intent;
	}

	private void initViews() {

		phone.setText(Utils.hidePhone(PreferencesUtils.getString(this, PreferencesUtils.USER_PHONE)));

		et_code.addTextChangedListener(watcher);

		btn_get_code = (Button) findViewById(R.id.btn_get_code);
		btn_get_code.setOnClickListener(this);

		password = (EditText) findViewById(R.id.password);
		password_re = (EditText) findViewById(R.id.password_re);

		password.addTextChangedListener(watcher);
		password_re.addTextChangedListener(watcher);

		save = (Button) findViewById(R.id.save);
		save.setOnClickListener(this);
	}

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (TextUtils.isEmpty(et_code.getText().toString())
					|| TextUtils.isEmpty(password.getText().toString())
					|| TextUtils.isEmpty(password_re.getText().toString())) {
				save.setEnabled(false);
			} else {
				save.setEnabled(true);
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_get_code:
			handler.postDelayed(runnable, 0);
			onRequest(SEND_REGISTER_VERIFY_CODE, UrlConstant.SENDSAFECENTERVERIFYCODE,
					HttpMethod.GET, null);
			break;
		case R.id.save:
			if (Utils.regPatternsPassword(this, password.getText().toString().trim())
					&& Utils.regPatternsPassword(this, password_re.getText().toString().trim())) {// 密码验证不通过

			} else if (!password.getText().toString().trim()
					.equals(password_re.getText().toString().trim())) {
				MyToast.showToast(this, "两次密码输入不一致");
			} else {
				RequestParams params = new RequestParams();
				params.addBodyParameter("mobileCode", et_code.getText().toString().trim());
				params.addBodyParameter("securityCode", password.getText().toString().trim());
				params.addBodyParameter("codeId", mCodeId);

				onRequest(SUBMIT, UrlConstant.SETSECURITYCODE, HttpMethod.POST, params);
			}

			break;
		default:
			break;
		}
	}

	public void onPostSuccess(int postId, String result) {
		Gson gson = new Gson();
		switch (postId) {
		case SEND_REGISTER_VERIFY_CODE:
			java.lang.reflect.Type type = new TypeToken<VerifyCode>() {
			}.getType();
			VerifyCode verifyCode = gson.fromJson(result, type);
			mCodeId = verifyCode.codeId;
			break;
		case SUBMIT:
			MyToast.showHMToast(this, true, "交易密码设置成功");
			finish();
			break;
		default:
			break;

		}
	}

	private int second = 60;
	Handler handler = new Handler();
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			if (second >= 0) {
				btn_get_code.setText(second-- + "s");
				btn_get_code.setEnabled(false);
			} else {
				btn_get_code.setText(R.string.register_get_code);
				btn_get_code.setEnabled(true);

				second = 60;

				Message message = new Message();
				message.what = 1;
				handlerStop.sendMessage(message);
			}

			handler.postDelayed(this, 1000);
		}
	};

	final Handler handlerStop = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				handler.removeCallbacks(runnable);
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (handlerStop != null && handler != null && runnable != null) {
			Message message = new Message();
			message.what = 1;
			handlerStop.sendMessage(message);
		}
	}

	@Override
	protected void setParams() {
		String title = getIntent().getStringExtra("title");
//		if (!TextUtils.isEmpty(title)) {
//			home_title_txt.setText("找回交易密码");
//		} else {
//			home_title_txt.setText("设置交易密码");
//		}
		home_title_txt.setText(title);
		home_title.setOnTouchListener(touch);
		initViews();

	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}
}
